/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
<template>
  <m-list-construction title="算力路由">
    <template slot="operation">
      <el-button
        type="primary"
        size="mini"
        @click="_create()"
      >创建路由</el-button>
      <el-dialog
        :title="item && item.name ? '编辑路由' : '创建路由'"
        :visible.sync="createDialog"
        width="auto"
        top="8vh"
        append-to-body
      >
        <m-create-route
          :item="item"
          @onUpdate="onUpdate"
          @close="close"
        ></m-create-route>
      </el-dialog>
    </template>
    <template slot="conditions">
      <m-conditions
        v-if="layout==='list'"
        @on-conditions="_onConditions"
      >
        <template slot="button-group">
          <el-button
            type="primary"
            plain
            size="mini"
            icon="el-icon-s-operation"
            @click="layout = 'view'"
            style="width:150px"
          >切换至拓扑图模式</el-button>
        </template>
      </m-conditions>
    </template>

    <template slot="content">
      <div
        v-if="layout==='view'"
        class="text-right"
        style="margin:12px 0 ;"
      >
        <el-button
          type="primary"
          plain
          size="mini"
          icon="el-icon-s-operation"
          @click="layout = 'list'"
          style="width:150px"
        >切换至列表模式</el-button>
      </div>
      <topologyMap v-if="layout==='view'"></topologyMap>
      <template v-if="(routeList.length || total>0) && layout==='list'">
        <m-list
          @on-edit="_onEdit"
          :environment-list="routeList"
          :page-no="searchParams.pageNum"
          :page-size="searchParams.pageSize"
        >

        </m-list>
        <div class="page-box">
          <el-pagination
            background
            @current-change="_page"
            @size-change="_pageSize"
            :page-size="searchParams.pageSize"
            :current-page.sync="searchParams.pageNum"
            :page-sizes="[10, 30, 50]"
            layout="sizes, prev, pager, next, jumper"
            :total="total"
          >
          </el-pagination>
        </div>
      </template>
      <template v-if="!routeList.length && total<=0 && layout==='list'">
        <m-no-data></m-no-data>
      </template>
      <m-spin
        :is-spin="isLoading"
        :is-left="isLeft"
      ></m-spin>
    </template>
  </m-list-construction>
</template>
<script>
  import _ from 'lodash'
  import { mapActions } from 'vuex'
  import mList from './_source/list'
  import topologyMap from './_source/topologyMap'
  // import store from '@/conf/home/store'
  import mSpin from '@/module/components/spin/spin'
  import mCreateRoute from './_source/createRoute'
  import mNoData from '@/module/components/noData/noData'
  import listUrlParamHandle from '@/module/mixin/listUrlParamHandle'
  import mConditions from '@/module/components/conditions/conditions'
  import mListConstruction from '@/module/components/listConstruction/listConstruction'

  export default {
    name: 'route-index',
    data () {
      return {
        total: null,
        isLoading: true,
        routeList: [],
        workerGroupList: [],
        environmentWorkerGroupRelationList: [],
        searchParams: {
          pageSize: 10,
          pageNum: 1,
          searchVal: ''
        },
        isLeft: true,
        item: {},
        createDialog: false,
        layout: 'list'
      }
    },
    mixins: [listUrlParamHandle],
    props: {},
    methods: {
      ...mapActions('router', ['getRouteListPaging']),
      /**
       * Query
       */
      _onConditions (o) {
        this.searchParams = _.assign(this.searchParams, o)
        this.searchParams.pageNum = 1
      },
      _page (val) {
        this.searchParams.pageNum = val
      },
      _pageSize (val) {
        this.searchParams.pageSize = val
      },
      _onEdit (item) {
        this.item = item
        this.createDialog = true
      },
      _create () {
        this.item = {}
        this.createDialog = true
      },
      onUpdate () {
        this._debounceGET('false')
        this.createDialog = false
      },
      close () {
        this.createDialog = false
      },
      _getList (flag) {
        if (sessionStorage.getItem('isLeft') === 0) {
          this.isLeft = false
        } else {
          this.isLeft = true
        }
        this.isLoading = !flag
        Promise.all([this.getRouteListPaging({ ...this.searchParams, name: this.searchParams.searchVal })]).then((values) => {
          console.log('fdasfas', values)
          if (this.searchParams.pageNum > 1 && values[0].rows.length === 0) {
            this.searchParams.pageNum = this.searchParams.pageNum - 1
          } else {
            this.routeList = []
            this.routeList = values[0].rows
            this.total = values[0].total
            this.isLoading = false
          }
        }).catch(e => {
          this.isLoading = false
        })
      }
    },
    watch: {
      // router
      '$route' (a) {
        // url no params get instance list
        this.searchParams.pageNum = _.isEmpty(a.query) ? 1 : a.query.pageNum
      }
    },
    beforeDestroy () {
      sessionStorage.setItem('isLeft', 1)
    },
    components: { mList, mListConstruction, mConditions, mSpin, mNoData, mCreateRoute, topologyMap }
  }
</script>
